package com.yuan.order.controller;

import com.alipay.api.AlipayApiException;
import com.yuan.order.entity.OrderInfoEntity;
import com.yuan.order.service.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Controller
@RequestMapping("order/orderinfo2")
public class OrderController {
    @Autowired
    OrderInfoService orderInfoService;
    @RequestMapping("/paySuccess")
    public String  success(HttpServletRequest request)throws AlipayApiException, UnsupportedEncodingException {
//        System.out.println("aaaaaaaaasdljaskaf暗示法多所噶所过付风噶付过付过过付过过过过过过");
// 获取支付宝GET过来反馈信息
        Map<String,String> params = new HashMap<String,String>();
        Map<String,String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用
            valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }

        String orderId = requestParams.get("out_trade_no")[0];
        System.out.println(orderId);
        OrderInfoEntity byId = orderInfoService.getById(orderId);
        byId.setStatus(1);  // 修改状态 已支付
        byId.setPayDate(new Date());  // 设置支付时间
        orderInfoService.updateById(byId);

        return "redirect:http://127.0.0.1:5500/order.html";
    }
}
